package com.ilop.sthome.ui.activity.mine;

import android.annotation.SuppressLint;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;

import com.example.common.base.BaseBindActivity;
import com.ilop.sthome.common.CommonId;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityWebViewBinding;

/**
 * @author skygge
 * @date 2019-12-03.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：浏览器预览
 */
public class WebViewActivity extends BaseBindActivity<ActivityWebViewBinding> {

    private String mTitle;
    private String privacy_agreement;
    private String instructions_url;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mTitle = getIntent().getStringExtra(CommonId.KEY_PRODUCT_KEY);
        privacy_agreement = getIntent().getStringExtra(CommonId.KEY_AGREEMENT);
        instructions_url = getIntent().getStringExtra(CommonId.KEY_INSTRUCTIONS);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void initView() {
        super.initView();
        WebSettings webSettings = mDBind.webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        mDBind.webView.setInitialScale(25);
        if (privacy_agreement!=null) {
            mDBind.webView.setWebViewClient(new WebViewClient());
            mDBind.webView.loadUrl(privacy_agreement);
        }else if (instructions_url!=null){
            webSettings.setAllowFileAccess(true);
            webSettings.setAllowFileAccessFromFileURLs(true);
            webSettings.setAllowUniversalAccessFromFileURLs(true);
            mDBind.webTitle.setText(mTitle);
            mDBind.webView.loadUrl("file:///android_asset/index.html?" + instructions_url);
        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivWebBack.setOnClickListener(view -> finish());
    }
}
